//
//  XLGPackFileUpChannelProtocol+HTTPChannelProtocol.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-4-29.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGPackFileUpChannelProtocol+HTTPChannelProtocol.h"

#import "XLGHTTPProtocolBase.h"

@interface UploadChunkHTTPProtocol : XLGHTTPProtocolBase

@end

@implementation UploadChunkHTTPProtocol
{
    XLGPackUpChunkObj* _chunk;
}

-(id)initWithPackupChunkObj:(XLGPackUpChunkObj*)chunk
{
    self = [super init];
    
    _chunk = chunk;
    
    _transformType = EProtocolTransformTypeHTTPSRESTFORFILE;
    
    return self;
}

- (void)writeRequestProtocol:(writeRequestProtocolBlock)protocol
{
    NSMutableData *data = [NSMutableData new];
    
    NSString *boundary = [NSString stringWithFormat:@"---------------------------%@",@"123aAabBbcCcdDd321"];
    
    [data appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSString * headerTemplate = @"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"; range=\"%@\"; chunknum=\"%d\"\r\nContent-Type: application/octet-stream\r\n\r\n";
    NSString * header = [NSString stringWithFormat:headerTemplate,_chunk.fileID,_chunk.fileName,_chunk.chunkRange,_chunk.chunkNum];
    [data appendData:[header dataUsingEncoding:NSUTF8StringEncoding]];
    
    [data appendData:_chunk.data];
    
    [data appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSString *param = @"";
    if(_chunk.param.count >0)
    {
        param = [NSString megerStringWithTag:_chunk.param value:^NSString *(id obj)
         {
             return [NSString stringWithFormat:@"%@",obj];
         } tag:@"&"];
    }
    NSString *url = [NSString stringWithFormat:@"attachment/MobileAttachment/Upload?pathKey=%@",param];
    
    protocol(url,XLGHTTPMethodTypePost,^NSDictionary*(NSDictionary* requestHead)
    {
        NSMutableDictionary *head = [requestHead mutableCopy];
        
        [head removeObjectForKey:@"Accept-Encoding"];
        
        [head setObject:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary] forKey:@"content-type"];
        
        return head;
        
    },nil,data);
}

- (void)readResponseProtocol:(NSDictionary *)jsonDictionary output:(NSObject **)obj
{
    if(jsonDictionary != nil)
    {
        if([jsonDictionary isKindOfClass:[NSArray class]])
        {
            NSArray *array = (NSArray*)jsonDictionary;
            NSString *URL = array[0];
        
            *obj = URL;
        }
    }
}

@end

@implementation XLGPackFileUpChannelProtocol (HTTPChannelProtocol)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

- (EProtocolTransformType)protocolTransformType
{
    return EProtocolTransformTypeHTTPSRESTFORFILE;
}

- (CProtocolBase*)uploadFileChunksProtocol:(XLGPackUpChunkObj*)packUpFileObj
{
    return [[UploadChunkHTTPProtocol alloc] initWithPackupChunkObj:packUpFileObj];
}

#pragma clang diagnostic pop

@end
